//
//  OldTwoViewController.m
//  Item A House
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "OldTwoViewController.h"
#import "OldModel.h"
#import "HidModel.h"
#import "UIImageView+WebCache.h"
#import "OldTwoTableViewCell.h"
#import "OldMapModel.h"
#import "OldCollectionModel.h"
#import "UIScrollView+PullLoad.h"
#import "MapViewController.h"
//#import "FeVietNamLoader.h"
@interface OldTwoViewController ()<UITableViewDataSource, UITableViewDelegate, HidModelDelegate>
{

}
@property (nonatomic, retain) HidModel *conn;
//@property (retain, nonatomic) FeVietNamLoader *vietNamLoader;
@end

@implementation OldTwoViewController
- (void)dealloc
{
    _conn.delegate = nil;
    [_conn release];
    [_table release];
    [_hid_id release];
    [_picURL release];
    [_array release];
    [_mapArray release];
    [super dealloc];
}

//http://m.goufang.com/api/ershou/eseinfoerp
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.mapArray = [NSMutableArray array];
    }
    return self;
}
- (void)connectFinished:(NSData *)data
{
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    //    NSLog(@"%@", dic);
    
    NSMutableArray *array = [dic objectForKey:@"esinfo"];
    for (NSMutableDictionary *dic in array) {
        OldModel *model = [[OldModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.array addObject:model];
    }
    NSMutableArray *mapArray = [dic objectForKey:@"esmap"];
    for (NSMutableDictionary *dic in mapArray) {
        OldMapModel *map = [[OldMapModel alloc] init];
        [map setValuesForKeysWithDictionary:dic];
        [self.mapArray addObject:map];
    }
    
    [self.table reloadData];
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height- 64 - 70) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = UITableViewCellStyleDefault;
    [self.view addSubview:self.table];
    [self.table release];
    
    OldTwoTableViewCell *oldMap = [[OldTwoTableViewCell alloc] init];
    UIImageView *map = (UIImageView *)[oldMap viewWithTag:1000];
    UITapGestureRecognizer *recogn1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newAction:)];
    [map addGestureRecognizer:recogn1];
    [self.table addSubview:map];
    [recogn1 release];
}
- (void)newAction:(UITapGestureRecognizer *)tap
{
    MapViewController *map = [[MapViewController alloc] init];
    [self.navigationController pushViewController:map animated:YES];
    [map release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 900;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"李狗蛋";
    OldTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[OldTwoTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify]autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    OldModel *model = [self.array objectAtIndex:indexPath.row];

    [cell.housepic sd_setImageWithURL:[NSURL URLWithString:self.picURL] placeholderImage:[UIImage imageNamed:@"10.jpg"]];
    
    cell.addrinfo.text = model.addrinfo;
    cell.areainfo.text = model.areainfo;
    cell.smallareaid.text = model.smallareaid;
    cell.v_price.text = [NSString stringWithFormat:@"%ld万", (long)model.v_price];
    cell.v_houselayout.text = model.v_houselayout;
    cell.v_housesize.text = model.v_housesize;
    cell.c_pricepersqm.text = [NSString stringWithFormat:@"单价:%@", model.c_pricepersqm];
    cell.houseward.text = [NSString stringWithFormat:@"朝向:%@", model.houseward];
    cell.fitment.text = [NSString stringWithFormat:@"装修:%@", model.fitment];
    cell.property.text = [NSString stringWithFormat:@"权属:%@", model.property];
    cell.title.text = model.title;
    cell.xuequ.text = [NSString stringWithFormat:@"学区:%@", model.xuequ];
    cell.xqaddress.text = [NSString stringWithFormat:@"学区地址:%@", model.xqaddress];
    cell.housefeature.text = [NSString stringWithFormat:@"房主说:%@", model.housefeature];
    if ([self.mapArray count] == 0) {
        return cell;
    }
    OldMapModel *mModel = [self.mapArray objectAtIndex:indexPath.row];
//    [cell.maping sd_setImageWithURL:[NSURL URLWithString:mModel.mapimg] placeholderImage:[UIImage imageNamed:@"10.jpg"]];
    cell.address.text = [NSString stringWithFormat:@"地址:%@", mModel.address];
    cell.way.text = [NSString stringWithFormat:@"公交:%@", mModel.way];
    cell.school.text = [NSString stringWithFormat:@"学校:%@", mModel.school];
    cell.hospital.text = [NSString stringWithFormat:@"医院:%@", mModel.hospital];
    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTable];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSLog(@"%@", self.hid_id);
    [dic setObject:@"dl" forKey:@"cityid"];
    [dic setObject:self.hid_id forKey:@"hid"];
    self.conn = [HidModel connectWithUrl:@"http://m.goufang.com/api/ershou/eseinfoerp" delegate:self parmaters:dic];
    
    // 收藏
    UIButton *collectionButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIImage *image = [UIImage imageNamed:@"My.png"];
    [collectionButton setImage:image forState:UIControlStateNormal];
    [collectionButton addTarget:self action:@selector(collectionButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:collectionButton];
}

- (void)collectionButton:(id)sender
{
    OldModel *model = [self.array lastObject];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:model.houseid forKey:@"houseid"];
    [dic setObject:model.addrinfo forKey:@"addrinfo"];
    [OldCollectionModel writeArray:dic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
